﻿using B.Auth.APi.Application.Command.Users;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand
{
    public class GetUserCommandHand : IRequestHandler<GetUsercommadn, ApiResult<List<UserDto>>>
    {
        private readonly IBaseRepository<User> _userdb;
        private readonly IBaseRepository<Province> _province;
        private readonly IBaseRepository<City> _city;
        private readonly IBaseRepository<County> _county;
        private readonly IBaseRepository<RoleModel> _role;
        private readonly IBaseRepository<RoleUser> _roleuser;

        public GetUserCommandHand(IBaseRepository<User> userdb, IBaseRepository<Province> province, IBaseRepository<City> city, IBaseRepository<County> county)
        {
            _userdb = userdb;
            _province = province;
            _city = city;
            _county = county;
        }

        public Task<ApiResult<List<UserDto>>> Handle(GetUsercommadn request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<List<UserDto>>();

            var list=new List<UserDto>();
            var user = _userdb.GetAll().ToList();
            var city=_city.GetAll().ToList();
            var county = _county.GetAll().ToList();
            var province=_province.GetAll().ToList();
     
            var users = from a in user
                        join b in province
                        on a.FromProvince equals b.Id
                        join c in city
                        on a.FromCity equals c.Id
                        join d in county
                        on a.FromCounty equals d.Id
                       
                        
                        where a.Del == 0
                        select new UserDto()
                        {

                            Id = a.Id,
                            Name = a.Name,
                            Sex = a.Sex,
                            FromProvince = a.FromProvince,
                            ProvinceName = b.ProvinceName,
                            FromCity = a.FromCity,
                            CityName = c.CityName,
                            FromCounty = a.FromCounty,
                            CountyName = d.CountyName,
                            Addressmessage = a.Addressmessage,
                            CreateTime = a.CreateTime,
                            UpdateTime = a.UpdateTime,
                            CreateUserName = a.CreateUserName,
                            UpdateUserName = a.UodateUserName,
                            departid=a.departid,

                           



                        };

           
            result.Code = ApiEnum.ok;
            result.data = users.ToList();
            return Task.FromResult(result);

        }


     

    }
}
